$title aggregate_benchmark



* ------------------------------------------------------------------------------
* set options
* ------------------------------------------------------------------------------

* default benchmark
$if not set benchmark_file $set benchmark_file "build\data\noaggr_benchmark.gdx"

* default aggregation
$if not set map_file $set map_file "build\aggregation_map\default_aggregation.gms"

* default output file
$if not set output_file $set output_file "data\default_aggregation.gdx"



* ------------------------------------------------------------------------------
* load the original model sets
* ------------------------------------------------------------------------------

* basic model sets
sets
  s                     sectors
  r                     regions
  h                     households
  trd                   trade markets
  f                     primary factors
  t                     time;

* Load sets
$gdxin %benchmark_file%
$load r s h trd f t

* set aliases
alias
  (s,ss);



* ------------------------------------------------------------------------------
* load the original benchmark
* ------------------------------------------------------------------------------

parameters

* benchmark data
  y0(r,s)               benchmark - output
  ld0(r,s)              benchmark - labor demand
  kd0(r,s)              benchmark - capital demand
  id0(r,ss,s)           benchmark - intermediate demand
  m0(r,s,trd)           benchmark - imports
  x0(r,s,trd)           benchmark - exports
  cd0(r,s,h)            benchmark - consumer demand
  i0(r,s)               benchmark - investment demand
  invh0(r,h)            benchmark - household investment
  g0(r,s)               benchmark - government demand
  le0(r,h)              benchmark - labor rental
  re0(r,h)              benchmark - capital rental
  n0(r,h)               benchmark - households

* benchmark tax rates
  tl0(r)                tax rate - labor use
  tk0(r)                tax rate - capital use
  tc0(r)                tax rate - consumption
  ty0(r,s)              tax rate - production;

* load benchmark IMPLAN data
$load id0 ld0 kd0 cd0 x0 m0 i0 g0 re0 le0 invh0 n0 ty0 tl0 tc0 tk0

* calculate output
y0(r,s) = (sum(ss, id0(r,ss,s))+(1+tl0(r))*ld0(r,s)+(1+tk0(r))*kd0(r,s))/(1-ty0(r,s));



* ------------------------------------------------------------------------------
* aggregate the benchmark
* ------------------------------------------------------------------------------

* load the aggregation mapping
$include "%map_file%"

* set aliases
alias
  (s_a,ss_a);

* aggregated benchmark
parameters

* benchmark data
  ld_a(r_a,s_a)         benchmark - labor demand
  kd_a(r_a,s_a)         benchmark - capital demand
  id_a(r_a,s_a,ss_a)    benchmark - intermediate demand
  m_a(r_a,s_a,trd)      benchmark - imports
  x_a(r_a,s_a,trd)      benchmark - exports
  cd_a(r_a,s_a,h_a)     benchmark - consumer demand
  i_a(r_a,s_a)          benchmark - investment demand
  invh_a(r_a,h_a)       benchmark - household investment
  g_a(r_a,s_a)          benchmark - government demand
  le_a(r_a,h_a)         benchmark - labor rental
  re_a(r_a,h_a)         benchmark - capital rental
  n_a(r_a,h_a)          benchmark - households

* benchmark tax rates
  tl_a(r_a)             tax rate - labor use
  tk_a(r_a)             tax rate - capital use
  tc_a(r_a)             tax rate - consumption
  ty_a(r_a,s_a)         tax rate - production;

parameters
  idt(r_a,s,s_a)        benchmark - intermediate demand - temporary step;

* aggregate benchmark accounts
idt(r_a,s,s_a)     = sum((r_map(r_a,r),s_map(s_a,ss)), id0(r,s,ss));
id_a(r_a,s_a,ss_a) = sum((s_map(s_a,s)), idt(r_a,s,ss_a));
ld_a(r_a,s_a)      = sum((r_map(r_a,r),s_map(s_a,s)), ld0(r,s));
kd_a(r_a,s_a)      = sum((r_map(r_a,r),s_map(s_a,s)), kd0(r,s));
x_a(r_a,s_a,trd)   = sum((r_map(r_a,r),s_map(s_a,s)), x0(r,s,trd));
m_a(r_a,s_a,trd)   = sum((r_map(r_a,r),s_map(s_a,s)), m0(r,s,trd));
g_a(r_a,s_a)       = sum((r_map(r_a,r),s_map(s_a,s)), g0(r,s));
i_a(r_a,s_a)       = sum((r_map(r_a,r),s_map(s_a,s)), i0(r,s));
cd_a(r_a,s_a,h_a)  = sum((r_map(r_a,r),s_map(s_a,s),h_map(h_a,h)), cd0(r,s,h));
re_a(r_a,h_a)      = sum((r_map(r_a,r),h_map(h_a,h)), re0(r,h));
le_a(r_a,h_a)      = sum((r_map(r_a,r),h_map(h_a,h)), le0(r,h));
invh_a(r_a,h_a)    = sum((r_map(r_a,r),h_map(h_a,h)), invh0(r,h));
n_a(r_a,h_a)       = sum((r_map(r_a,r),h_map(h_a,h)), n0(r,h));

* set tax rates by maintaining revenue
tl_a(r_a)          = sum((r_map(r_a,r),s), tl0(r)*ld0(r,s))/sum(s_a, ld_a(r_a,s_a));
tk_a(r_a)          = sum((r_map(r_a,r),s), tk0(r)*kd0(r,s))/sum(s_a, kd_a(r_a,s_a));
tc_a(r_a)          = sum((r_map(r_a,r),s,h), tc0(r)*cd0(r,s,h))/sum((s_a,h_a), cd_a(r_a,s_a,h_a));

* set production tax rate to equate output levels between datasets
ty_a(r_a,s_a)      = (sum((r_map(r_a,r),s_map(s_a,s)), ty0(r,s)*y0(r,s))
                      /sum((r_map(r_a,r),s_map(s_a,s)), y0(r,s)))
                     $sum((r_map(r_a,r),s_map(s_a,s)), y0(r,s));

* if it's a single region benchmark then there is no domestic trade
if(card(r_a) eq 1,
  x_a(r_a,s_a,"dtrd") = 0;
  m_a(r_a,s_a,"dtrd") = 0;
);

* Save the aggregated benchmark
execute_unload "%output_file%", s_a=s, r_a=r, h_a=h, trd, f, t, id_a=id0, ld_a=ld0,
                                kd_a=kd0, cd_a=cd0, x_a=x0, m_a=m0, i_a=i0, g_a=g0,
                                re_a=re0, le_a=le0, invh_a=invh0, n_a=n0, ty_a=ty0,
                                tl_a=tl0, tc_a=tc0, tk_a=tk0;
